<!--
 * @Description: helloworld
 * @Author: zugelu
 * @Date: 2021-07-27 13:58:29
 * @LastEditors: zugelu
 * @LastEditTime: 2021-07-30 10:03:28
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="test"></div>
</body>
<script src="../js/17.0.1/react.development.js"></script>
<script src="../js/17.0.1/react-dom.development.js"></script>
<script src="../js/17.0.1/babel.min.js"></script>

<script type="text/babel">
    class Parent extends React.Component {
        constructor(props) {
            console.log('Parent - constructor');
            super(props);
            this.state = { num: 1 }
        }

        add = () => {
            let { num } = this.state;
            this.setState({ num: num + 1 })
        }

        // 卸载组件的回调
        death = () => {
            ReactDOM.unmountComponentAtNode(document.getElementById("test"));
        }

        // 强制更新组件的回调
        force = () => {
            this.forceUpdate();
        }

        // 组件将要挂载钩子
        UNSAFE_componentWillMount = () => {
            console.log('Parent - componentWillMount');
        }

        render() {
            return (
                <div>
                    <h3 >这是求和的结果：{this.state.num}</h3>
                    <button onClick={this.add}>点击+1</button><br />
                    <button onClick={this.death}>卸载组件</button><br />
                    <button onClick={this.force}>强制更新组件，不做任何更改</button><br />
                    <hr />
                    <Child num={this.state.num} />
                </div>
            )
        }
    }

    class Child extends React.Component {

        // 组件将要接收新的props的钩子
        UNSAFE_componentWillReceiveProps() {
            console.log("Child - componentWillREceiveProps");
        }

        // 控制组件更新的“阀门”
        shouldComponentUpdate = () => {
            console.log('Child - shouldComponentUpdate');
            return true;
        }

        // 组件将要更新钩子
        UNSAFE_componentWillUpdate = () => {
            console.log('Child - componentWillUpdate');
        }

        // 组件更新完毕钩子
        componentDidUpdate = () => {
            console.log('Child - componentDidUpdate');
        }


        render() {
            console.log("Child - render");
            return (
                <div>
                    我接收父组件的传值：{this.props.num}
                </div>
            )
        }
    }


    ReactDOM.render(<Parent />, document.querySelector("#test"))
</script>

</html>